﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PMS.BusinessFacade;
using PMS.Common;
using PMS.EntityQuery;
using PMS.EntityManage;
using System.Data.SqlClient;

public partial class Modules_Flow_FlowAdd : System.Web.UI.Page
{

    IPerf_FlowHandler flowHandler = new Perf_FlowManage();


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!(Request["ID"] != null && Request["ID"] != string.Empty))
        {
            if (!CheckPurviews())
            {
                Response.Redirect("~/Login.aspx");
            }
        }
        if (!IsPostBack)
        {
            dataBind();
        }
    }

    private bool CheckPurviews()
    {
        if (Session["UserPurviews"] != null)
        {
            string strPur = Session["UserPurviews"].ToString();
            if (strPur.Contains("401010") )
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }

    private void CheckUpdatePurviews()
    {
        if (Session["UserPurviews"] != null)
        {
            string strPur = Session["UserPurviews"].ToString();
            if (strPur.Contains("401011"))
            {
                txtCode.ReadOnly = false;
                txtName.ReadOnly = false;
                txtRemark.ReadOnly = false;
                btnSave.Enabled = true;
            }
            else
            {
                txtCode.ReadOnly = true;
                txtName.ReadOnly = true;
                txtRemark.ReadOnly = true;
                btnSave.Enabled = false;
            }
        }
        else
        {
            txtCode.ReadOnly = true;
            txtName.ReadOnly = true;
            txtRemark.ReadOnly = true;
            btnSave.Enabled = false;
        }
    }

    private void dataBind()
    {
        if (Request["ID"] != null && Request["ID"] != string.Empty)
        {
            CheckUpdatePurviews();

            Perf_FlowQuery query = flowHandler.GetPerf_FlowQuery(int.Parse(Request["ID"]));
            if (query != null)
            {
                txtCode.Text = query.Code;
                txtName.Text = query.Name;
                txtRemark.Text = query.Remark;
            }
        }
    }

    private bool IsExistNameUpdate(string name, int id)
    {
        bool istrue = false;
        DataTable dt = flowHandler.GetPerf_FlowList("IsEnable=@IsEnable and Name=@Name and ID!=@ID", new SqlParameter[] { new SqlParameter("@IsEnable", Enums.NormalStatus.Vallid.ToString("d")), new SqlParameter("@Name", name), new SqlParameter("@ID", id.ToString()) });
        if (dt != null && dt.Rows.Count > 0)
        {
            istrue = true;
        }
        return istrue;
    }

    public bool IsExistName(string name)
    {
        bool istrue = false;
        DataTable dt = flowHandler.GetPerf_FlowList("IsEnable=@IsEnable and Name=@Name ", new SqlParameter[] { new SqlParameter("@IsEnable", Enums.NormalStatus.Vallid.ToString("d")), new SqlParameter("@Name", name) });
        if (dt != null && dt.Rows.Count > 0)
        {
            istrue = true;
        }
        return istrue;
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        Perf_FlowQuery query = new Perf_FlowQuery();
        if (Request["ID"] != null && Request["ID"] != string.Empty)
        {
            query = flowHandler.GetPerf_FlowQuery(int.Parse(Request["ID"]));
            if (query != null)
            {
                if (IsExistNameUpdate(txtName.Text.Trim(), query.ID))
                {
                    Jscript.Alert("流程名称已经存在,请重新输入!", this.Page);
                    return;
                }
            }

        }
        else
        {
            if (IsExistName(txtName.Text.Trim()))
            {
                Jscript.Alert("流程名称已经存在,请重新输入!", this.Page);
                return;
            }
        }

        query.Code = txtCode.Text.Trim();
        query.Name = txtName.Text.Trim();
        query.Remark = txtRemark.Text.Trim();
        query.IsEnable = int.Parse(Enums.NormalStatus.Vallid.ToString("d"));
        if (Request["ID"] != null && Request["ID"] != string.Empty)
        {
            if (flowHandler.UpdatePerf_Flow(query))
            {
                Jscript.Alert("操作成功!", this.Page);
                Jscript.ReturnAndClose(this.Page, "true");
            }
            else
            {
                Jscript.Alert("操作失败!", this.Page);
            }

        }
        else
        {
            if (flowHandler.InsertPerf_Flow(query))
            {
                Jscript.Alert("操作成功!", this.Page);
                Jscript.ReturnAndClose(this.Page, "true");
            }
            else
            {
                Jscript.Alert("操作失败!", this.Page);
            }
        }
    }


}
